package com.xiaoyun.xpath.handler.field;

import cn.wanghaomiao.xpath.model.JXDocument;
import com.xiaoyun.xpath.HandlerUtils;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

public class DateFieldHandler<T> extends StringFieldHandler<T> {

    private static Map<String, String> patterns = new HashMap<>();

    static {
        patterns.put("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$", "YYYY-MM-DD HH:mm:ss");
        patterns.put("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$", "YYYY-MM-DD");
        patterns.put("((((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$", "HH:mm:ss");

        patterns.put("^\\d{4}-\\d{1,2}-\\d{1,2}", "YYYY-MM-DD");
        patterns.put("^\\d{4}\\d{1,2}\\d{1,2}", "YYYYMMDD");
        patterns.put("^\\d{4} \\d{1,2} \\d{1,2}", "YYYY MM DD");
    }

    @Override
    protected <T> void stringMethodInvoke(Class<T> clazz, JXDocument jxDocument, T t, String fieldName, String xpathMapping) throws Exception {
        List<Object> list = jxDocument.sel(xpathMapping);
        String content = list.get(0).toString();
        Date date = isDate(content);
        if (date != null) {
            String methodName = HandlerUtils.getMethodName(fieldName);
            Method method = clazz.getDeclaredMethod(methodName, Date.class);
            method.invoke(t, date);
        }


    }

    protected Date isDate(String content) {
        String patternOk = null;

        Iterator<String> keyIterator = patterns.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            boolean isMatch = Pattern.matches(key, content);
            if (isMatch) {
                patternOk = patterns.get(key);
                break;
            }
        }

        if (patternOk != null) {
            DateFormat df = new SimpleDateFormat(patternOk);
            Date date = null;
            try {
                date = df.parse(content);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
            return date;
        }
        return null;
    }

    @Override
    public String getType() {
        return Date.class.getName();
    }

}